package cn.wolfcode.wolf2w.business.test;

import cn.wolfcode.wolf2w.business.service.LogisticsStrategy;
import cn.wolfcode.wolf2w.business.test.service.impl.BuyerMessageStrategy;
import cn.wolfcode.wolf2w.business.test.service.impl.WeighStrategy;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Demo {

    public static void main(String[] args) {

        // 订单信息
        TradeOrder tradeOrder1 = new TradeOrder();
        tradeOrder1.setId(1L);
        tradeOrder1.setTradeNo("JY1");
        tradeOrder1.setBuyerMessage("发顺丰");
        tradeOrder1.setWeight(new BigDecimal("11"));

        TradeOrder tradeOrder2 = new TradeOrder();
        tradeOrder2.setId(2L);
        tradeOrder2.setTradeNo("JY2");
        tradeOrder2.setBuyerMessage("发note");
        tradeOrder2.setWeight(new BigDecimal("6"));

        List<TradeOrder> tradeOrders = new ArrayList<>();
        tradeOrders.add(tradeOrder1);
        tradeOrders.add(tradeOrder2);

        // 根据买家留言自动选快递规则
        BuyerMessageRule buyerMessageRule1 = new BuyerMessageRule();
        buyerMessageRule1.setId(1L);
        buyerMessageRule1.setKeyWord("顺丰");
        buyerMessageRule1.setLogisticsId(1L);
        BuyerMessageRule buyerMessageRule2 = new BuyerMessageRule();
        buyerMessageRule2.setId(2L);
        buyerMessageRule2.setKeyWord("note");
        buyerMessageRule2.setLogisticsId(2L);
        List<BuyerMessageRule> buyerMessageRules = new ArrayList<>();
        buyerMessageRules.add(buyerMessageRule1);
        buyerMessageRules.add(buyerMessageRule2);

        // 根据货品重量自动选快递规则
        WeighRule weighRule1 = new WeighRule();
        weighRule1.setId(1L);
        weighRule1.setWeightStart(new BigDecimal("5"));
        weighRule1.setWeightEnd(new BigDecimal("10"));
        weighRule1.setLogisticsId(1L);
        WeighRule weighRule2 = new WeighRule();
        weighRule2.setId(2L);
        weighRule2.setWeightStart(new BigDecimal("10"));
        weighRule2.setWeightEnd(new BigDecimal("15"));
        weighRule2.setLogisticsId(2L);

        List<WeighRule> weighRules = new ArrayList<>();
        weighRules.add(weighRule1);
        weighRules.add(weighRule2);

        for (TradeOrder tradeOrder : tradeOrders) {
            for (BuyerMessageRule buyerMessageRule : buyerMessageRules) {
                if (tradeOrder.getBuyerMessage().contains(buyerMessageRule.getKeyWord())){
                    System.out.println(buyerMessageRule.getId()+"发"+buyerMessageRule.getKeyWord());
                }
            }
        }
        // TODO 优先根据备注，其次根据重量为每个订单选上快递
    }


}


//package cn.wolfcode.wolf2w.business.test;
//
//import cn.wolfcode.wolf2w.business.service.LogisticsStrategy;
//import cn.wolfcode.wolf2w.business.test.service.impl.BuyerMessageStrategy;
//import cn.wolfcode.wolf2w.business.test.service.impl.WeighStrategy;
//
//import java.math.BigDecimal;
//import java.util.ArrayList;
//import java.util.List;
//
//public class Demo {
//
//    public static void main(String[] args) {
//
//        // 订单信息
//        TradeOrder tradeOrder1 = new TradeOrder();
//        tradeOrder1.setId(1L);
//        tradeOrder1.setTradeNo("JY1");
//        tradeOrder1.setBuyerMessage("发顺丰");
//        tradeOrder1.setWeight(new BigDecimal("11"));
//
//        TradeOrder tradeOrder2 = new TradeOrder();
//        tradeOrder2.setId(2L);
//        tradeOrder2.setTradeNo("JY2");
//        tradeOrder2.setBuyerMessage("发中通");
//        tradeOrder2.setWeight(new BigDecimal("6"));
//
//        List<TradeOrder> tradeOrders = new ArrayList<>();
//        tradeOrders.add(tradeOrder1);
//        tradeOrders.add(tradeOrder2);
//
//        // 买家留言规则
//        List<LogisticsStrategy> buyerMessageStrategies = new ArrayList<>();
//        buyerMessageStrategies.add(new BuyerMessageStrategy("顺丰", 1L));
//        buyerMessageStrategies.add(new BuyerMessageStrategy("中通", 2L));
//
//        // 货品重量规则
//        List<LogisticsStrategy> weighStrategies = new ArrayList<>();
//        weighStrategies.add(new WeighStrategy(new BigDecimal("5"), new BigDecimal("10"), 1L));
//        weighStrategies.add(new WeighStrategy(new BigDecimal("10"), new BigDecimal("15"), 2L));
//
//        // 为每个订单选快递
//        for (TradeOrder tradeOrder : tradeOrders) {
//            // 首先根据买家留言匹配快递
//            Long logisticsId = matchLogistics(buyerMessageStrategies, tradeOrder);
//            if (logisticsId == null) {
//                // 如果买家留言没有匹配到快递，则根据货品重量匹配快递
//                logisticsId = matchLogistics(weighStrategies, tradeOrder);
//            }
//            tradeOrder.setLogisticsId(logisticsId);
//            System.out.println("订单号：" + tradeOrder.getTradeNo() + "，选中的快递ID：" + logisticsId);
//        }
//    }
//
//    private static Long matchLogistics(List<LogisticsStrategy> strategies, TradeOrder order) {
//        for (LogisticsStrategy strategy : strategies) {
//            if (strategy.match(order)) {
//                return strategy.getLogisticsId();
//            }
//        }
//        return null;
//    }
//}
